package Persistance;

import java.util.Hashtable;
import java.util.List;

import Model.Cereal;
import Model.CerealCosechaFina;
import Model.CerealCosechaGruesa;
import Model.Cosecha;
import Model.ItemMineralLote;
import Model.Lote;
import Model.LoteCompuesto;
import Model.LoteSimple;
import Model.Mineral;
import Model.Propietario;
import Model.Siembra;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class AdministradorPersistencia
{
	private static AdministradorPersistencia sharedInstance;
	private Hashtable<String, IDataMapper> dataMapers;
	
	private AdministradorPersistencia()
	{
		dataMapers = new Hashtable<String, IDataMapper>();
		
		//Agrego los dataMappers que voy a utilizar
		dataMapers.put(Propietario.class.toString(), new PropietarioDataMaper());
		dataMapers.put(Cereal.class.toString(), new CerealDataMapper());
		dataMapers.put(CerealCosechaFina.class.toString(), new CerealDataMapper());
		dataMapers.put(CerealCosechaGruesa.class.toString(), new CerealDataMapper());
		dataMapers.put(Cosecha.class.toString(), new CosechaDataMapper());
		dataMapers.put(ItemMineralLote.class.toString(), new ItemMineralLoteDataMapper());
		dataMapers.put(Lote.class.toString(), new LoteDataMapper());
		dataMapers.put(LoteSimple.class.toString(), new LoteDataMapper());
		dataMapers.put(LoteCompuesto.class.toString(), new LoteDataMapper());
		dataMapers.put(Mineral.class.toString(), new MineralDataMapper());
		dataMapers.put(Siembra.class.toString(), new SiembraDataMapper());
	}
	
	private IDataMapper getMapperForObject(Object o)
	{
		String className = o.getClass().toString();
		return getMapperForClass(className);
	}
	
	private IDataMapper getMapperForClass(String className)
	{
		IDataMapper mapper = dataMapers.get(className);
		
		return mapper;
	}
	
	public static AdministradorPersistencia getAdministradorPersistencia()
	{
		if (sharedInstance == null)
			sharedInstance = new AdministradorPersistencia();
		
		return sharedInstance;
	}
	
	public boolean insert(Object o)
	{
		IDataMapper mapper = this.getMapperForObject(o);
		return mapper.insert(o);
	}
	
	public boolean update(Object o)
	{
		IDataMapper mapper = this.getMapperForObject(o);
		return mapper.update(o);
	}
	
	public boolean delete(Object o)
	{
		IDataMapper mapper = this.getMapperForObject(o);
		return mapper.delete(o);
	}
	
	public Object findById(Object o)
	{
		IDataMapper mapper = this.getMapperForObject(o);
		return mapper.findById(o);
	}

	public List<?> findAll(String className)
	{
		IDataMapper mapper = this.getMapperForClass(className);
		return mapper.findAll();
	}
	
	public static String booleanToString(boolean b){
		if(b){
			return "T";
		}
		return "F";
	}
	
	public static boolean stringToBoolean(String s){
		if(s.equalsIgnoreCase("T")){
			return true;
		}
		return false;
	}
}
